package com.ruoyi.project.kbjz.service.impl;

import com.ruoyi.project.kbjz.domain.KbjzPatient;
import com.ruoyi.project.kbjz.mapper.KbjzPatientMapper;
import com.ruoyi.project.kbjz.service.IKbjzPatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Random;

@Service
public class KbjzPatientServiceImpl implements IKbjzPatientService {
    @Autowired
    private KbjzPatientMapper patientMapper;
    @Override
    public KbjzPatient selectPatientByPatientCard(String patientCard) {
        KbjzPatient kbjzPatient = patientMapper.selectPatientByPatientCard(patientCard);
        if (kbjzPatient == null){
            return null;
        }
        Date date = new Date();
        int year = date.getYear() - kbjzPatient.getPatientBirthday().getYear();
        kbjzPatient.setPatientAge(year);
        return kbjzPatient;
    }

    @Override
    public int addPatient(KbjzPatient kbjzPatient) {
        //设置患者编号，当前时间+随机数（1000——9999）+患者性别编号
        Date date = new Date();
        int random = new Random().nextInt(9000)+1000;
        String patientId = "HZ"+date.getTime()+random+kbjzPatient.getPatientSex();
        kbjzPatient.setPatientId(patientId);
        kbjzPatient.setPatientInfoStatus(0);
        return patientMapper.addPatient(kbjzPatient);
    }
}
